﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Artizan.Utility;

/// <summary>
/// Summary description for PropertyImage
/// </summary>

public class PropertyImage
{
    public int WebPropertiesAttributeID { get; set; }
    public int PomplingAddressID { get; set; }
    public string DisplayPicturePath { get; set; }
    //public string picturePathOnHD { get; set; }
    public string PictureTitle { get; set; }
    public bool IsMainImage { get; set; }
    public string PomplingCustomerID { get; set; }

    public PropertyImage()
    {
    }

    public PropertyImage(string pomplingCustomerID, int pomplingAddressID, bool isMainImage, string displayPicturePath) 
    { 
        this.PomplingCustomerID = pomplingCustomerID;
        this.PomplingAddressID = pomplingAddressID;
        this.IsMainImage = isMainImage;
        this.DisplayPicturePath = displayPicturePath;
    }

    public void SaveImagePath(string CrudType )
    {
        string connectionString = Globals.GetConnectionString();
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("PomPropertyImagePathAlter", conn))
            {
                try
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure; //will not update, no WebPropertyID set
                    cmd.Parameters.AddWithValue("@pPomplingAddressID", this.PomplingAddressID);
                    cmd.Parameters.AddWithValue("@pDisplayPicturePath", this.DisplayPicturePath);
                    cmd.Parameters.AddWithValue("@pPictureTitle", (string.IsNullOrEmpty( this.PictureTitle)) ? (object) DBNull.Value: this.PictureTitle);
                    cmd.Parameters.AddWithValue("@pIsMainImage", this.IsMainImage);
                    cmd.Parameters.AddWithValue("@pCrudType", CrudType);
                    cmd.Parameters.AddWithValue("@pPomplingCustomerID", this.PomplingCustomerID);
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }

    public void DeleteImagePath()
    {
        this.SaveImagePath("DELETE_IMAGE");
    }

    public static List<PropertyImage> SelectAdditionalImages(int pomplingAddressID)
    {
        string connectionString = Globals.GetConnectionString();
        List<PropertyImage> propertyImageList = new List<PropertyImage>();
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("PomPropertiesAdditionalImagesSelect", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pPomplingAddressID", pomplingAddressID);
                cmd.Connection.Open();

                DataTable dt = new DataTable("DtPropertyImageList");
                IDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
                cmd.Connection.Close();
                propertyImageList = FieldMapper.DataTableToList<PropertyImage>(ref dt);
            }
            return propertyImageList;
        }
    }

    public static string ImageSaveWebPath()
    {
         string folderName = @"houses/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Month.ToString();
         return folderName;
    }

    public static string ImageSaveFilePath()
    {
        string folderName = @"houses\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + @"\" + DateTime.Now.Month.ToString();
        return folderName;
    }

    public static List<PropertyImage> SelectAllPropertyImages(int pomplingAddressID)
    {
        string connectionString = Globals.GetConnectionString();
        List<PropertyImage> propertyImageList = new List<PropertyImage>();
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("PomPropertyImagesSelect", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pPomplingAddressID", pomplingAddressID);
                cmd.Connection.Open();

                DataTable dt = new DataTable("DtPropertyImageList");
                IDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
                cmd.Connection.Close();
                propertyImageList = FieldMapper.DataTableToList<PropertyImage>(ref dt);
            }
            return propertyImageList;
        }
    }

    public static List<PropertyImage> SelectMainImage(int addressID)
    {
        string connectionString = Globals.GetConnectionString();
        List<PropertyImage> propertyImageList = new List<PropertyImage>();
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("PomPropertyImageSelectMainImage", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pPomplingAddressID", addressID);
                cmd.Connection.Open();

                DataTable dt = new DataTable("DtPropertyImageList");
                IDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
                cmd.Connection.Close();
                propertyImageList = FieldMapper.DataTableToList<PropertyImage>(ref dt);
            }
            return propertyImageList;
        }
    }
}

